# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import ToscrapebookItem
from scrapy.loader import ItemLoader


class Books3Spider(CrawlSpider):
    name = 'books3'
    allowed_domains = ['books.toscrape.com']
    start_urls = ['http://books.toscrape.com/']

    rules = (
        Rule(LinkExtractor(restrict_xpaths="//li[@class='next']"), follow=True),
        Rule(LinkExtractor(restrict_xpaths="//article//div[@class='image_container']"), callback='parse_item'),
    )

    def parse_item(self, response):
        book_load = ItemLoader(item = ToscrapebookItem(), response = response)
        book_load.add_xpath('bookname', "//h1/text()")
        book_load.add_xpath('bookprice', "//table/tr[3]/td/text()")
        book_load.add_xpath('bookstock', "//*[re:match(text(),'stock \([0-9].')]/text()")
        yield book_load.load_item()
